using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyException : System.Exception
{
    public MyException() {}
    public MyException(string message) : base(message) {}
}

public class DiskAttributes : MonoBehaviour
{
    public int score;
    public float speedX;
    public float speedY;
}

public class DiskFactory : MonoBehaviour
{
    // List<GameObject> used; 
    // List<GameObject> free; // 可用的飞碟
    System.Random rand;
    // Start is called before the first frame update
    void Start()
    {
        // used = new List<GameObject>();
        // free = new List<GameObject>();
        rand = new System.Random();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public GameObject GetDisk(int round) {
        GameObject disk;
        int score = rand.Next(1, 4);
        // if (free.Count != 0) {
        //     disk = free[0];
        //     free.Remove(disk);
        // }
        if (score == 1)
            disk = GameObject.Instantiate(Resources.Load("Prefabs/red", typeof(GameObject))) as GameObject;
        else if (score == 2)
            disk = GameObject.Instantiate(Resources.Load("Prefabs/green", typeof(GameObject))) as GameObject;
        else 
            disk = GameObject.Instantiate(Resources.Load("Prefabs/purple", typeof(GameObject))) as GameObject;
        disk.AddComponent<DiskAttributes>(); // 添加属性控件

        // 根据不同round设置DiskAttr值
        DiskAttributes diskAttributes = disk.GetComponent<DiskAttributes>();
        diskAttributes.score = score;
        // 根据不同分数决定速度，颜色和大小等
        diskAttributes.speedX = (rand.Next(1, 6) + diskAttributes.score + round) * 0.3f;
        diskAttributes.speedY = (rand.Next(1, 6) + diskAttributes.score + round) * 0.3f;

        if (diskAttributes.score == 1) {
            disk.transform.localScale = new Vector3(12f, 12f, 12f);
        }
        else if (diskAttributes.score == 2) {
            disk.transform.localScale = new Vector3(10f, 10f, 10f);
        }
        else {
            disk.transform.localScale = new Vector3(8f, 8f, 8f);
        }

        // 飞碟从四个不同方向飞入
        int dec = rand.Next(1, 5);
        Debug.Log("dec: " + dec);
        if (dec == 1) { // 左上
            disk.transform.position = new Vector3(-12f, 8f, 1f);
            diskAttributes.speedY *= -1;
        } 
        else if (dec == 2) { // 左下
            disk.transform.position = new Vector3(-12f, -4f, 1f);
        }
        else if (dec == 3) { // 右上
            disk.transform.position = new Vector3(12f, 8f, 1f);
            diskAttributes.speedX *= -1;
            diskAttributes.speedY *= -1;
        }
        else { // 右下
            disk.transform.position = new Vector3(12f, -4f, 1f);
            diskAttributes.speedX *= -1;
        }
        // used.Add(disk);
        disk.SetActive(true);
        return disk;
    }

    public void FreeDisk(GameObject disk) {
        disk.SetActive(false);
        Destroy(disk);
        Debug.Log("free disk");
    }

}
